package com.study.sxc.controller;

import com.study.sxc.Entity.Staff;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.staffService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/staff")
public class StaffController {

    @Resource
    private staffService staffService;

    @GetMapping("/get")
    public Response<List<Staff>> getStaffs() {
        List<Staff> staffs;
        try {
            staffs = staffService.getAllStaffs();
        } catch (Exception e) {
            return Response.<List<Staff>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info("查询异常: " + e.getMessage())
                    .data(null)
                    .build();
        }
        if(staffs == null || staffs.isEmpty()) {
            return Response.<List<Staff>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(staffs)
                    .build();
        }
        return Response.<List<Staff>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(staffs)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addStaff(@RequestBody Staff staff) {
        // 校验必要字段
        if (staff.getStaffName() == null || staff.getStaffName().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("员工姓名不能为空")
                    .build();
        }
        if (staff.getContact() == null || staff.getContact().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("联系方式不能为空")
                    .build();
        }
        if (staff.getRole() == null || staff.getRole().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("职位不能为空")
                    .build();
        }
        int result = staffService.addStaff(staff);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateStaff(@RequestBody Staff staff) {
        // 校验必要字段
        if (staff.getStaffId() == null || staff.getStaffId() <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("员工ID不能为空且必须为正整数")
                    .build();
        }
        if (staff.getStaffName() == null || staff.getStaffName().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("员工姓名不能为空")
                    .build();
        }
        if (staff.getContact() == null || staff.getContact().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("联系方式不能为空")
                    .build();
        }
        if (staff.getRole() == null || staff.getRole().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("职位不能为空")
                    .build();
        }
        int result = staffService.updateStaff(staff);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteStaff(@RequestBody Staff staff) {
        Integer id = staff.getStaffId();
        if (id == null || id <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("员工ID不能为空且必须为正整数")
                    .build();
        }
        int result = staffService.deleteStaff(id);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @GetMapping("/get/{id}")
    public Response<Staff> getStaffById(@PathVariable Integer id) {
        // 根据id获取员工信息
        Staff staff = staffService.getStaffById(id);
        if (staff == null) {
            return Response.<Staff>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<Staff>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(staff)
                .build();
    }
} 